package com.huang.foundation.controller;


import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResultBuilder;
import com.huang.foundation.entity.respose.OSSResponse;
import com.huang.foundation.entity.to.AliYunOSSDTO;
import com.huang.foundation.service.AliYunSTSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author HuangShen
 * @Description
 * @create 2021-10-13 22:53
 */
@RestController
@RequestMapping("/public")
@Api("oss相关接口")
public class AliYunSTSController {

    @Resource
    private AliYunSTSService aliYunSTSService;


    @ApiOperation(value = "获取oss 临时访问token")
    @GetMapping("/ossToken")
    public HStringResult<OSSResponse> getOssToken(@ApiParam("项目名") @RequestParam("project") String project,
                                     @ApiParam("文件类型") @RequestParam("type") String type) {

        AliYunOSSDTO aliYunOSSDTO = this.aliYunSTSService.getOSSToken(type, project);
        if (Objects.isNull(aliYunOSSDTO)) return ResultBuilder.failHSingle();
        OSSResponse ossResponse = OSSResponse.builder()
                .accessKeyId(aliYunOSSDTO.getAccessKeyId())
                .accessKeySecret(aliYunOSSDTO.getAccessKeySecret())
                .stsToken(aliYunOSSDTO.getSecurityToken())
                .bucket(aliYunOSSDTO.getBucket())
                .callbackUrl(aliYunOSSDTO.getCallbackUrl())
                .expiration(aliYunOSSDTO.getExpiration())
                .host(aliYunOSSDTO.getHost())
                .region(aliYunOSSDTO.getRegion())
                .build();
        return ResultBuilder.successHSingle(ossResponse);

    }


}
